package com.chen.pspms.controller;

import com.chen.pspms.entity.Choice;
import com.chen.pspms.entity.CompanyInfo;
import com.chen.pspms.entity.Majorhazardregistertable;
import com.chen.pspms.service.CompanyService;
import com.chen.pspms.service.MajorhazardregisterService;
import com.chen.pspms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/administractorOther")
public class AdministractorOtherController {
    @Autowired
    CompanyService companyService;
    @Autowired
    MajorhazardregisterService majorhazardregisterService;
    @Autowired
    UserService userService;

    @RequestMapping("/warning")
    public String list(Model model){
        List<CompanyInfo> companylist = companyService.findAll();
        List<Majorhazardregistertable> majorhazardregisterList=new ArrayList<>();
        for(CompanyInfo company:companylist){
            List<Majorhazardregistertable> allMajorhazardregisterList=majorhazardregisterService.findyListByComId(company.getId());
            for(Majorhazardregistertable majorhazardregistertable:allMajorhazardregisterList){
                if(majorhazardregistertable.getLevel().equals("预警")){
                    majorhazardregisterList.add(majorhazardregistertable);
                }
            }
        }
        model.addAttribute("majorhazardregisterList",majorhazardregisterList);
        return "预警防控/企业风险预警";
    }

    @RequestMapping("/dangerous")
    public String dangerous(Model model){
        List<CompanyInfo> companylist = companyService.findAll();
        List<Majorhazardregistertable> majorhazardregisterList=new ArrayList<>();
        for(CompanyInfo company:companylist){
            List<Majorhazardregistertable> allMajorhazardregisterList=majorhazardregisterService.findyListByComId(company.getId());
            for(Majorhazardregistertable majorhazardregistertable:allMajorhazardregisterList){
                if(!majorhazardregistertable.getLevel().equals("预警") && !majorhazardregistertable.getLevel().equals("无")){
                    majorhazardregisterList.add(majorhazardregistertable);
                }
            }
        }
        model.addAttribute("majorhazardregisterList",majorhazardregisterList);
        return "预警防控/智能识别和自动生成风险管控清单";
    }

    @RequestMapping("/style")
    public String style(Model model){
        return "安全生产隐患排查和重大危险源统计功能/指定格式";
    }

    @RequestMapping("/forms")
    public String forms(Model model,@RequestParam(value="checkbox")String[] list){
        List<Majorhazardregistertable> majorhazardregisterList=majorhazardregisterService.findyAll();
        Choice choice=toChoise(list);
        model.addAttribute("majorhazardregisterList",majorhazardregisterList);
        model.addAttribute("choice",choice);
        return "安全生产隐患排查和重大危险源统计功能/生成报表";
    }

    private Choice toChoise(String[] list){
        Class<?> clazz=Choice.class;
        try{
            Object object=clazz.newInstance();
            for(String fieldName:list){
                Field field=clazz.getDeclaredField(fieldName);
                field.setAccessible(true);
                field.set(object,true);
            }
            return (Choice)object;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
